package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetmealMapper setmealMapper;

    @Override
    @Transactional
    public Integer add(ShoppingCartDTO shoppingCartDTO) {
        //tag1是更新shopping_cart表中记录返回值,tag2是插入shopping_cart返回值
        Integer tag1=null,tag2=null;
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();

        //根据userId,dishId,setmealId,dishFlavor,查询shopping_cart表中是否已经存有该菜品/套餐
        ShoppingCart shoppingCart = shoppingCartMapper.selectByList(userId, dishId, setmealId, dishFlavor);  //selectByList

        //判断shopping_cart表中是否已经存有该菜品/套餐
        if (shoppingCart != null) {
            //若有,则number++,其他不变,
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);

            //更新shopping_cart表中记录
            tag1 = shoppingCartMapper.updateNumber(shoppingCart);
        } else {
            //若没有,则新建一条记录插入到shopping_cart表中
            String name = null, image = null;
            BigDecimal amount = null;
            if (dishId == null) {
                //加入购物车的是套餐
                SetmealVO setmealVO = setmealMapper.selectById(setmealId);

                //获取name
                name = setmealVO.getName();

                //获取image
                image = setmealVO.getImage();

                //获取amount
                amount = setmealVO.getPrice();
            } else if (shoppingCartDTO.getSetmealId() == null) {
                //加入购物车的是菜品
                Dish dish = dishMapper.selectById(dishId);

                //获取name
                name = dish.getName();

                //获取image
                image = dish.getImage();

                //获取amount
                amount = dish.getPrice();
            }

            //封装ShoppingCart对象
            shoppingCart = ShoppingCart.builder()
                    .name(name)
                    .image(image)
                    .userId(userId)
                    .dishId(dishId)
                    .setmealId(setmealId)
                    .dishFlavor(dishFlavor)
                    .number(1)
                    .amount(amount)
                    .createTime(LocalDateTime.now())
                    .build();
            //插入shopping_cart表中
            tag2 = shoppingCartMapper.insert(shoppingCart);
        }
        return tag1!=null?tag1:tag2;
    }

    @Override
    public List<ShoppingCart> findAll() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.selectAll(userId);
    }

    @Override
    public Integer clean() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.deleteAll(userId);
    }

    @Override
    @Transactional
    public Integer removeOne(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();
        ShoppingCart shoppingCart = shoppingCartMapper.selectByList(userId, dishId, setmealId, dishFlavor);
        Integer number = shoppingCart.getNumber();
        if(number==1){
            return shoppingCartMapper.deleteOne(userId,dishId,setmealId,dishFlavor);
        }else{
            shoppingCart.setNumber(number+1);
            return shoppingCartMapper.updateNumber(shoppingCart);
        }
    }
}